A process that needs to use the resource executes a "P" operation on the semaphore. This operation tests and decrements the count in the semaphore. If the count is greater than zero before the operation, at least one resource unit is available. The count is reduced by 1 and the process continues executing. When the count is not greater than zero, the process is blocked until a resource unit is available; then it continues. In either case, following a P operation, the process knows that it has exclusive use of a resource unit.
When it finishes its work, the process releases the resource by executing a "V" operation on the semaphore. This operation increments the count. It also unblocks any process that might be blocked in a P operation, waiting for the resource. If more than one process is waiting, the one that has waited longest is released first (FIFO order).
Tip: Useful mnemonics for P and V: P depletes the resource. V revives it. IRIX supports two forms of semaphore: SVR4-compatible, and Silicon Graphics.
To acquire a resource, blocking if it is not available, a process applies the uspsema() call to the semaphore. To test the resource, acquiring it if it is available but not blocking when it is in use, a process can call uscpsema(). To release the resource, a process calls usvsema().
IRIX also supports a parallel set of "pollable" semaphores. The P operation on a pollable semaphores does not block when the resource is in use. Instead, it returns a flag value, and the process must use the poll() system call to find out when a V operation has made the resource available.
IRIX semaphores support "metering" (use counts) and debug tracing. You can turn either facility on and off dynamically. By metering a semaphore, you can find out how often processes actually block in a P operation. This can reveal whether or not a resource is a bottleneck to performance.
For more information on semaphores, refer to
Like a shared memory segment (see "SVR4-Compatible Shared Memory"), a set of semaphores is somewhat like a file in that it
SVR4-compatible semaphores do not support the conventional P and V operations. Instead, the semop() system call supplies a wider range of operations, including incrementing and decrementing counts by more than 1. The semop() call supports concurrent operations on multiple semaphores at once. This is convenient in some cases because it allows you to claim more than one resource simultaneously, without danger of deadlock.
For sample code and more information on SVR4-compatible semaphores, refer to
Tip: If you require portability, use SVR4-compatible semaphores. Otherwise, the IRIX semaphore implementation is faster, has more features, and works with the IRIX shared-memory implementation.